<html>
<head>
  <title>03random库</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="Evernote Windows/307027 (zh-CN, DDL); Windows/6.1.0 (Win32);"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="744"/>
<h1>03random库</h1>

<div>
<span><div><strong><span lang="EN-US" style="CoLor: red" xml:lang="EN-US">random.random</span></strong> <span lang="EN-US" style="CoLor: red" xml:lang="EN-US">.</span></div><div><p style="TexT-inDenT: 15.75pt; mso-char-indent-count: 1.5"><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">用于产生一个</span><span lang="EN-US" xml:lang="EN-US">0</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">到</span><span lang="EN-US" xml:lang="EN-US">1</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">之间的浮点数。</span><span lang="EN-US" xml:lang="EN-US">0&lt;=n&lt;1.0</span></p><p style="TexT-inDenT: 15.75pt; mso-char-indent-count: 1.5"><span lang="EN-US" xml:lang="EN-US">&gt;&gt;&gt; import random</span></p><p style="TexT-inDenT: 15.75pt; mso-char-indent-count: 1.5"><span lang="EN-US" xml:lang="EN-US">&gt;&gt;&gt; random.random()</span></p><p style="TexT-inDenT: 15.75pt; mso-char-indent-count: 1.5"><span lang="EN-US" xml:lang="EN-US">0.060899158939506703</span></p></div><div><br/></div><div> <strong><span style="CoLor: red">random.uniform</span></strong></div><div><div style="MArGin-LeFT: 15.75pt; TexT-inDenT: -15.75pt; mso-char-indent-count: -1.5"><span lang="EN-US" xml:lang="EN-US">random.uniform</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">的函数原型为：</span><span lang="EN-US" xml:lang="EN-US">random.uniform(a, b)</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">，用于生成一个指定范围内的随机符点数，两个参数其中一个是上限，一个是下限。如果</span><span lang="EN-US" xml:lang="EN-US">a &gt; b</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">，则生成的随机数</span><span lang="EN-US" xml:lang="EN-US">n:  b &lt;= n &lt;= a</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">。如果</span> <span lang="EN-US" xml:lang="EN-US">a &lt;b</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">，</span> <span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">则</span> a <span lang="EN-US" xml:lang="EN-US">&lt;= n &lt;= b</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">。</span></div><p style="TexT-inDenT: 15.75pt; mso-char-indent-count: 1.5"><span lang="EN-US" xml:lang="EN-US">&gt;&gt;&gt; random.uniform(1,10)</span></p><p style="TexT-inDenT: 15.75pt; mso-char-indent-count: 1.5"><span lang="EN-US" xml:lang="EN-US">3.7359858841440392</span></p><p style="TexT-inDenT: 15.75pt; mso-char-indent-count: 1.5"><span lang="EN-US" xml:lang="EN-US">&gt;&gt;&gt; random.uniform(8,2)</span></p><p style="TexT-inDenT: 15.75pt; mso-char-indent-count: 1.5"><span lang="EN-US" xml:lang="EN-US">4.0493468953165124</span></p></div><div><strong><span lang="EN-US" style="CoLor: red" xml:lang="EN-US">random.randint</span></strong></div><div><span lang="EN-US" xml:lang="EN-US">random.randint()</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">的函数原型为：</span><span lang="EN-US" xml:lang="EN-US">random.randint(a, b)</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">，用于生成一个指定范围内的整数。其中参数</span><span lang="EN-US" xml:lang="EN-US">a</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">是下限，参数</span><span lang="EN-US" xml:lang="EN-US">b</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">是上限，生成的随机数</span><span lang="EN-US" xml:lang="EN-US">n: a &lt;= n &lt;= b</span></div><div><p><strong><span lang="EN-US" style="CoLor: #ff9900" xml:lang="EN-US"><span style="mso-spacerun: yes"> </span></span> <span lang="EN-US" xml:lang="EN-US"><span style="mso-spacerun: yes"> </span></span></strong><strong><span lang="EN-US" style="FonT-WeiGHT: normal; mso-bidi-font-weight: bold" xml:lang="EN-US">&gt;&gt;&gt; random.randint(1,4)</span></strong></p><p style="TexT-inDenT: 15.45pt; mso-char-indent-count: 1.47"><strong><span lang="EN-US" style="FonT-WeiGHT: normal; mso-bidi-font-weight: bold" xml:lang="EN-US">4</span></strong></p><p style="TexT-inDenT: 15.45pt; mso-char-indent-count: 1.47"><strong><span lang="EN-US" style="FonT-WeiGHT: normal; mso-bidi-font-weight: bold" xml:lang="EN-US">&gt;&gt;&gt; random.randint(1,4)</span></strong></p><p style="TexT-inDenT: 15.45pt; mso-char-indent-count: 1.47"><strong><span lang="EN-US" style="FonT-WeiGHT: normal; mso-bidi-font-weight: bold" xml:lang="EN-US">3</span></strong></p></div><div><br/></div><div><strong><span lang="EN-US" style="CoLor: red" xml:lang="EN-US">random.randrange</span></strong><span lang="EN-US" style="CoLor: red" xml:lang="EN-US"><br/></span><span lang="EN-US" xml:lang="EN-US">random.randrange</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">的函数原型为：</span><span lang="EN-US" xml:lang="EN-US">random.randrange([start], stop[, step])</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">，从指定范围内，按指定基数递增的集合中</span> <span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">获取一个随机数。如：</span><span lang="EN-US" xml:lang="EN-US">random.randrange(10, 100, 2)</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">，结果相当于从</span><span lang="EN-US" xml:lang="EN-US">[10, 12, 14, 16, ... 96, 98]</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">序列中获取一个随机数。</span><span lang="EN-US" xml:lang="EN-US">random.randrange(10, 100, 2)</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">在结果上与</span> <span lang="EN-US" xml:lang="EN-US">random.choice(range(10, 100, 2)</span> <span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">等效。</span></div><div><p style="TexT-inDenT: 20.35pt; mso-char-indent-count: 1.94"><span lang="EN-US" xml:lang="EN-US">&gt;&gt;&gt; random.randrange(10)</span></p><p style="MArGin-LeFT: 15.75pt; TexT-inDenT: 5.25pt; mso-char-indent-count: .5; mso-para-margin-left: 1.5gd"><span lang="EN-US" xml:lang="EN-US">3</span></p><p style="MArGin-LeFT: 15.75pt; TexT-inDenT: 5.25pt; mso-char-indent-count: .5; mso-para-margin-left: 1.5gd"><span lang="EN-US" xml:lang="EN-US">&gt;&gt;&gt; random.randrange(1,12,2)</span></p><p style="MArGin-LeFT: 15.75pt; TexT-inDenT: 5.25pt; mso-char-indent-count: .5; mso-para-margin-left: 1.5gd"><span lang="EN-US" xml:lang="EN-US">11</span></p></div><div><br/></div><div style="TexT-inDenT: 21pt; mso-char-indent-count: 2.0"><div><span lang="EN-US" xml:lang="EN-US"> </span></div></div><div><strong><span lang="EN-US" style="CoLor: red" xml:lang="EN-US">random.choice</span></strong><span lang="EN-US" style="CoLor: red" xml:lang="EN-US"><br/></span><span lang="EN-US" xml:lang="EN-US">random.choice</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">从序列中获取一个随机元素。其函数原型为：</span><span lang="EN-US" xml:lang="EN-US">random.choice(sequence)</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">。参数</span><span lang="EN-US" xml:lang="EN-US">sequence</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">表示一个有序类型</span><span lang="EN-US" xml:lang="EN-US">,</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">即</span><span lang="EN-US" xml:lang="EN-US">list, tuple,</span> <span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">字符串都属于</span><span lang="EN-US" xml:lang="EN-US">sequence</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">。</span></div><div><p style="TexT-inDenT: 15.75pt; mso-char-indent-count: 1.5"><strong><span lang="EN-US" style="FonT-WeiGHT: normal; mso-bidi-font-weight: bold" xml:lang="EN-US">&gt;&gt;&gt; random.choice([1,3,5,7])</span></strong></p><p style="TexT-inDenT: 15.75pt; mso-char-indent-count: 1.5"><strong><span lang="EN-US" style="FonT-WeiGHT: normal; mso-bidi-font-weight: bold" xml:lang="EN-US">1</span></strong></p><p style="TexT-inDenT: 15.75pt; mso-char-indent-count: 1.5"><strong><span lang="EN-US" style="FonT-WeiGHT: normal; mso-bidi-font-weight: bold" xml:lang="EN-US">&gt;&gt;&gt; random.choice((1,3,5,7))</span></strong></p><p style="TexT-inDenT: 15.75pt; mso-char-indent-count: 1.5"><strong><span lang="EN-US" style="FonT-WeiGHT: normal; mso-bidi-font-weight: bold" xml:lang="EN-US">7</span></strong></p><p style="TexT-inDenT: 15.75pt; mso-char-indent-count: 1.5"><strong><span lang="EN-US" style="FonT-WeiGHT: normal; mso-bidi-font-weight: bold" xml:lang="EN-US">&gt;&gt;&gt; random.choice('helloworld')</span></strong></p><p style="TexT-inDenT: 15.75pt; mso-char-indent-count: 1.5"><strong><span lang="EN-US" style="FonT-WeiGHT: normal; mso-bidi-font-weight: bold" xml:lang="EN-US">'h'</span></strong></p></div><div><b><span style="color: red;"><br/></span></b></div><div><strong><span lang="EN-US" style="CoLor: red" xml:lang="EN-US">random.shuffle</span></strong></div><div><span lang="EN-US" xml:lang="EN-US">random.shuffle</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">的函数原型为：</span><span lang="EN-US" xml:lang="EN-US">random.shuffle(x[, random])</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">，用于将一个列表中的元素打乱。</span></div><div><p style="TexT-inDenT: 15.75pt; mso-char-indent-count: 1.5"><strong><span lang="EN-US" style="FonT-WeiGHT: normal; mso-bidi-font-weight: bold" xml:lang="EN-US">&gt;&gt;&gt; p=['This','is','python']</span></strong></p><p style="TexT-inDenT: 15.75pt; mso-char-indent-count: 1.5"><strong><span lang="EN-US" style="FonT-WeiGHT: normal; mso-bidi-font-weight: bold" xml:lang="EN-US">&gt;&gt;&gt; random.shuffle(p)</span></strong></p><p style="TexT-inDenT: 15.75pt; mso-char-indent-count: 1.5"><strong><span lang="EN-US" style="FonT-WeiGHT: normal; mso-bidi-font-weight: bold" xml:lang="EN-US">&gt;&gt;&gt; p</span></strong></p><p style="TexT-inDenT: 15.75pt; mso-char-indent-count: 1.5"><strong><span lang="EN-US" style="FonT-WeiGHT: normal; mso-bidi-font-weight: bold" xml:lang="EN-US">['This', 'python', 'is']</span></strong></p></div><div><b><span style="color: red;"><br/></span></b></div><div><strong><span lang="EN-US" style="CoLor: red" xml:lang="EN-US">random.sample</span></strong></div><div><p style="MArGin-LeFT: 15.75pt; mso-para-margin-left: 1.5gd"><span lang="EN-US" xml:lang="EN-US">random.sample</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">的函数原型为：</span><span lang="EN-US" xml:lang="EN-US">random.sample(sequence, k)</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">，从指定序列中随机获取指定长度的片断。</span><span lang="EN-US" xml:lang="EN-US">sample</span><span style="FonT-FAMiLY: 宋体; mso-ascii-font-family: 'Times new roman'; mso-hansi-font-family: 'Times new roman'">函数不会修改原有序列。</span></p><p style="MArGin-LeFT: 15.75pt; mso-para-margin-left: 1.5gd"><strong><span lang="EN-US" style="FonT-WeiGHT: normal; mso-bidi-font-weight: bold" xml:lang="EN-US">&gt;&gt;&gt; list1=[1,2,3,4,5,6,7,8,9]</span></strong></p><p style="MArGin-LeFT: 15.75pt; mso-para-margin-left: 1.5gd"><strong><span lang="EN-US" style="FonT-WeiGHT: normal; mso-bidi-font-weight: bold" xml:lang="EN-US">&gt;&gt;&gt; p=random.sample(list1,3)</span></strong></p><p style="MArGin-LeFT: 15.75pt; mso-para-margin-left: 1.5gd"><strong><span lang="EN-US" style="FonT-WeiGHT: normal; mso-bidi-font-weight: bold" xml:lang="EN-US">&gt;&gt;&gt; p</span></strong></p><p style="MArGin-LeFT: 15.75pt; mso-para-margin-left: 1.5gd"><strong><span lang="EN-US" style="FonT-WeiGHT: normal; mso-bidi-font-weight: bold" xml:lang="EN-US">[7, 8, 3]</span></strong></p></div><div><br/></div><div style="mso-para-margin-left: 1.5gd;margin-left:21px;"><br/></div></span>
</div></body></html> 